
local module = _G["BlinkHealthTextModule"]

if select(2, UnitClass("player")) ~= "WARRIOR" or not module then return end

local tcopy
local config_added = false

local L_SWORD_AND_BOARD = "검과 방패"
local L_SUDDEN_DEATH = "급살"
local L_TASTE_FOR_BLOOD = "피의 맛"
local L_EXECUTE = "마무리 일격"
local L_WEAKENED_ARMOR = "방어구 약화"
local L_BLOODSURGE = "피의 쇄도"
local L_RAGING_BLOW = "분노의 강타"
local L_RAGING_BLOW1 = "분노의 강타!"
local L_WARRIOR_CONFIG = "전사 설정"
local L_USE_BLOODSURGE = "|TInterface\\Icons\\Ability_Warrior_Bloodsurge:20|t피의 쇄도 발동시 |TInterface\\Icons\\Spell_Warrior_Wildstrike:20|t난폭한 일격을 표시합니다."
local L_USE_RAGING_BLOW = "|TInterface\\Icons\\Warrior_Wild_Strike:20|t분노의 강타가 발동시 표시합니다."
local L_USE_SUDDEN_DEATH = "|TInterface\\Icons\\Ability_Warrior_Improveddisciplines:20|t급살 발동시 |TInterface\\Icons\\INV_Sword_48:20|t마무리 일격을 표시합니다."
local L_USE_SWORD_AND_BOARD = "|TInterface\\Icons\\Ability_Warrior_Swordandboard:20|t검과 방패 발동시 |TInterface\\Icons\\INV_Shield_05:20|t방패 밀쳐내기를 표시합니다."


local defaultDB = {
	db_ver = 1.0,
	use_sudden_death = true,
	use_bloodsurge = true,
	use_raging_blow = true,
	use_sword_and_board = true,
}

module.config = {
	[L_SWORD_AND_BOARD] = {
		spellid = 46953
	},
	[L_TASTE_FOR_BLOOD] = {
		spellid = 56638
	},
	[L_SUDDEN_DEATH] = {
		spellid = 29725
	},
	[L_RAGING_BLOW] = {
		spellid = 85288
	},
}

-------------------------------------------------------------------------------
-- local functions
-------------------------------------------------------------------------------

function tcopy(to, from) -- "to" must be a table (possibly empty)
	for k,v in pairs(from) do
		if(type(v)=="table") then
			if not to then to = {} end
			to[k] = {}
			tcopy(to[k], v)
		else
			to[k] = v
		end
	end
end

local function getSize(baseSize, expirationTime, duration)
	local size = (expirationTime-GetTime()) / duration
	if size < 0.3 then
		size = baseSize * .8
	elseif size < 0.6 then
		size = baseSize * 1.0
	else
		size = baseSize * 1.2
	end
	return size / 2
end

-------------------------------------------------------------------------------
-- module functions
-------------------------------------------------------------------------------

function module:init()
	if self.addon.db then
		if not self.addon.db.class or not self.addon.db.class.db_ver or self.addon.db.class.db_ver < defaultDB.db_ver then
			self.addon.db.class = {}
			tcopy(self.addon.db.class, defaultDB)
		end
	end
	self.addon.mainFrame:RegisterEvent("PLAYER_ALIVE")
	self.addon.mainFrame:RegisterEvent("LEARNED_SPELL_IN_TAB")
	self.addon.mainFrame:RegisterEvent("CHARACTER_POINTS_CHANGED")
	self.addon.mainFrame:RegisterEvent("GLYPH_ADDED")
	self.addon.mainFrame:RegisterEvent("GLYPH_REMOVED")
	--self.addon.mainFrame:RegisterEvent("VARIABLES_LOADED")
	self:update()

	if not config_added then
		self:AddMiscConfig{
			type = "description",
			text = L_WARRIOR_CONFIG,
			fontobject = "QuestTitleFont",
			r = 1,
			g = 0.82,
			b = 0,
			justifyH = "LEFT",
		}
		self:AddMiscConfig{
			type = "toggle",
			text = L_USE_SUDDEN_DEATH,
			tooltip = L_USE_SUDDEN_DEATH,
			get = function ()
				if self.addon and self.addon.db then
					return self.addon.db.class.use_sudden_death
				end
				return
			end,
			set = function (value)
				self.addon.db.class.use_sudden_death = value
			end,
		}
		self:AddMiscConfig{
			type = "toggle",
			text = L_USE_BLOODSURGE,
			tooltip = L_USE_BLOODSURGE,
			get = function ()
				if self.addon and self.addon.db then
					return self.addon.db.class.use_bloodsurge
				end
				return
			end,
			set = function (value)
				self.addon.db.class.use_bloodsurge = value
			end,
		}
		self:AddMiscConfig{
			type = "toggle",
			text = L_USE_RAGING_BLOW,
			tooltip = L_USE_RAGING_BLOW,
			get = function ()
				if self.addon and self.addon.db then
					return self.addon.db.class.use_raging_blow
				end
				return
			end,
			set = function (value)
				self.addon.db.class.use_raging_blow = value
			end,
		}
		self:AddMiscConfig{
			type = "toggle",
			text = L_USE_SWORD_AND_BOARD,
			tooltip = L_USE_SWORD_AND_BOARD,
			get = function ()
				if self.addon and self.addon.db then
					return self.addon.db.class.use_sword_and_board
				end
				return
			end,
			set = function (value)
				self.addon.db.class.use_sword_and_board = value
			end,
		}
		config_added = true
	end

end

function module:update()
	self.primary = GetSpecialization()
	for auraName, t in pairs(self.config) do
		if t.talent and t.talent > 0 then
			local name, iconTexture, tier, column, selected, available = GetTalentInfo(t.talent);
			if name and available then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		else
			self.config[auraName].use = false
			if IsPlayerSpell(t.spellid) then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		end
	end
end

function module:LEARNED_SPELL_IN_TAB(...)
	self:update()
end
module.CHARACTER_POINTS_CHANGED = module.LEARNED_SPELL_IN_TAB
module.GLYPH_ADDED = module.LEARNED_SPELL_IN_TAB
module.GLYPH_REMOVED = module.LEARNED_SPELL_IN_TAB
function module:PLAYER_ALIVE(...)
	self.addon.mainFrame:UnregisterEvent("PLAYER_ALIVE")
	self:update()
end

function module:getTargetText()
	local size
	local icon, text = "", ""
	local aura, rank, icon, count, debufType, duration, expirationTime

	if self.config[L_SUDDEN_DEATH].use and self.addon.db.class.use_sudden_death then
		aura, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("player", L_SUDDEN_DEATH, "HELPFUL")
		if aura then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|TInterface\\Icons\\INV_Sword_48:%d|t"):format(size)
		end
	end

	if self.addon.db.class.use_bloodsurge then
		aura, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("player", L_BLOODSURGE, "HELPFUL")
		if aura then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|TInterface\\Icons\\Spell_Warrior_Wildstrike:%d|t"):format( size)
		end
	end

	if self.config[L_SUDDEN_DEATH].use and self.addon.db.class.use_raging_blow then
		aura, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("player", L_RAGING_BLOW1, "HELPFUL")
		if aura then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|TInterface\\Icons\\Warrior_Wild_Strike:%d|t"):format( size)
		end
	end

	if self.config[L_SWORD_AND_BOARD].use and self.addon.db.class.use_sword_and_board then
		aura, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_SWORD_AND_BOARD, nil, "HELPFUL")
		if aura then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|TInterface\\Icons\\INV_Shield_05:%d|t"):format(size) -- Shield Slam
		end
	end

	return text
end
